package de.kimera.controller;

import java.util.LinkedList;
import java.util.List;

import javax.swing.JDialog;

import de.kimera.api.IController;
import de.kimera.api.IEmail;
import de.kimera.api.IEmployee;
import de.kimera.api.IServicePlan;
import de.kimera.api.IShift;
import de.kimera.api.ITableOfHours;
import de.kimera.view.SendEmailDialog;

//singleton pattern
public class Controller implements IController {

	private static IController instance;
	
	private IServicePlan servicePlan;
	
	public static IController getInstance(){
		if(instance == null){
			instance = new Controller();
		}
		return instance;
	}
	
	//Constructor 
	private Controller(){}
	
	//Controller API Implementations
	
	@Override
	public void loadPDFDocument(String filePath) {
		servicePlan = PDFManager.loadPDFDocument(filePath); 
	}

	@Override
	public List<String> getEmployeeNames() {
		List<String> employeeNames = new LinkedList<String>();
		for(ITableOfHours tableOfHours : servicePlan.getTableOfHours()){
			employeeNames.add(tableOfHours.getEmployee().getServiceName());
		}
		return employeeNames;
	}

	@Override
	public String getServiceDateString() {
		return servicePlan.getMonthString()+" "+servicePlan.getYear();
	}
	
	@Override
	public List<IShift> getShifts(String employeeName) {
		List<IShift> shifts = new LinkedList<IShift>();
		
		for(ITableOfHours tableOfHours : servicePlan.getTableOfHours()){
			IEmployee employee = tableOfHours.getEmployee();
			if(employee.getServiceName().equals(employeeName)){
				shifts.addAll(tableOfHours.getShifts());
			}
		}
		
		return shifts;
	}

	@Override
	public IEmployee getEmployee(String employeeName) {
		for(ITableOfHours tableOfHours : servicePlan.getTableOfHours()){
			IEmployee employee = tableOfHours.getEmployee();
			if(employee.getServiceName().equals(employeeName)){
				return employee;
			}
		}

		return null;
	}

	@Override
	public void storeEmployeeData(IEmployee newEmployee) {
		EmployeeManager.storeEmployee(newEmployee);
	}

	@Override
	public void generatePDFReport(String employeeName, List<IShift> shifts) {
		PDFManager.generatePDFReport(employeeName, shifts);
	}

	public void sendEmail(IEmail email) {
		SendEmailDialog dialog = new SendEmailDialog(email);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setVisible(true);
	}
}
